
package eft.android.view;

import me.croute.calendarexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import eft.android.controller.Controller;


public class AddWorkout extends Activity{
	
	private Controller controller;
	private View view;
	
	private String date;
	
	private String title;
	private String descrip;
	private String weight;
	private String reps;
	private String time;
	private String notes;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.addworkout);
			
			Bundle extras = getIntent().getExtras();
			
			if( extras != null ){
				date = extras.getString(this.getPackageName() + ".date");
				title = extras.getString(this.getPackageName() + ".title");
				descrip = extras.getString(this.getPackageName() + ".descrip");
				weight = extras.getString(this.getPackageName() + ".weight");
				reps = extras.getString(this.getPackageName() + ".reps");
				time = extras.getString(this.getPackageName() + ".time");
				notes = extras.getString(this.getPackageName() + ".notes");
			}
			
			controller = Controller.getInstance();
			
			// Initialize the Model, View, and Controller classes
			//	final CalcManager myModel = new CalcManager();
			//		final MyView myView = new MyView(this);
			//	MyController myController =
			//		new MyController(this, myModel, myView);
			//	ViewGroup container = 
			//		(ViewGroup) findViewById(R.id.myLayout);
			//	container.addView(myView);
			//	container.addView(myController); 
			
			initVars();
			
			
		}
		catch (Exception e) {
			Log.e("ERROR", "ERROR IN CODE: " + e.toString());
			e.printStackTrace();
		}
	}
	

	private void initVars() {

		final Button buttonCancel = (Button) findViewById(R.id.addWorkoutCancel);

		buttonCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				controller.addWorkoutCancel();
			}
		}); 

		final Button buttonSave = (Button) findViewById(R.id.addWorkoutSave);
		buttonSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				controller.storeUserWorkout(getwoTitle(), getDescrip(), getReps(), getWeight(), getTime(), getNotes() );
			}
		}); 

		final Button buttonAddWorkout = (Button) findViewById(R.id.addWorkoutAddWorkout);
		buttonAddWorkout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//			myManager.doVariable(buttonVarY.getText().toString());
				//			myView.setView(myManager.getValue());
				controller.addWorkout(getwoTitle(), getDescrip(), getReps(), getWeight(), getTime(), getNotes() );
			}
		});	

		final Button cfWorkout = (Button) findViewById(R.id.addWorkoutcfWorkout);
		cfWorkout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//			myManager.doVariable(buttonVarZ.getText().toString());
				//			myView.setView(myManager.getValue());
				controller.selectCFWorkout( date );
			}
		});
		
		
		
		if( title != null){
			setwoTitle(title);
		}
		if( descrip != null){
			setDescrip(descrip);
		}
		if( weight != null || time != null || reps != null){
			setRwt(reps, weight, time);
		}
		if( notes != null){
			setNotes(notes);
		}
		
		
	}

	public boolean setwoTitle( String s )
	{
		TextView field = (TextView) findViewById(R.id.addWorkoutTitle);
		field.setText(s);
		return true;
		
	}

	public String getwoTitle()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutTitle);
		return (String) txt.getText();
	}

	public boolean setDescrip( String s )
	{
		TextView field = (TextView) findViewById(R.id.addWorkoutDescription);
		field.setText(s);
		return true;
		
	}

	public String getDescrip()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutDescription);
		return (String) txt.getText();
	}

	public boolean setRwt( String r, String w, String t )
	{
		TextView repsField = (TextView) findViewById(R.id.addWorkoutReps);

		TextView weightsField = (TextView) findViewById(R.id.addWorkoutWeight);

		TextView timeField = (TextView) findViewById(R.id.addWorkoutTime);

		repsField.setText(r);
		weightsField.setText(w);
		timeField.setText(t);

		return true;
	}

	public String getReps()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutReps);
		return (String) txt.getText();
	}

	public String getWeight()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutWeight);
		return (String) txt.getText();
	}


	public String getTime()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutTime);
		return (String) txt.getText();
	}
	
	
	public boolean setNotes( String s )
	{
		TextView field = (TextView) findViewById(R.id.addWorkoutNotes);
		field.setText(s);
		return true;
		
	}

	public String getNotes()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutNotes);
		return (String) txt.getText();
	}
}


